/*
 * @Author: your name
 * @Date: 2020-08-12 15:15:08
 * @LastEditTime: 2020-08-20 15:34:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \shici_demo\assets\Script\sceneMgr.ts
 */

import { Game } from "./game/game"
import { Result } from "./result/result"
import { audioMgr } from "./audioMgr"

const {ccclass, property} = cc._decorator;

@ccclass
export class SceneMgr extends cc.Component {

    @property(cc.Node)
    public homeNode: cc.Node = null;
    @property(cc.Node)
    public gameNode: cc.Node = null;
    @property(Game)
    public game: Game = null;
    @property(cc.Node)
    public resultNode: cc.Node = null;
    @property(Result)
    public result: Result = null;

    @property(cc.Label)
    public lv_progress_label: cc.Label = null;

    @property(audioMgr)
    public audiomgr: audioMgr = null;

    // ==============进度条start=============================
    @property(cc.Node)
    public targetNode: cc.Node = null;
    @property(cc.Node)
    public loadingNode: cc.Node = null;
    @property(cc.ProgressBar)
    public progressbar: cc.Node = null;
    @property(cc.Label)
    public progressLabel: cc.Label = null;
    public comDegress: number = 0;  // 完成度
    public comFlag = false;         // 完成标志
    public proBar: cc.ProgressBar = null;
    public nowTime: number = 0;
    public allTime: number = 10;
    // ==============进度条end=============================

    onLoad() {
        /** ----进度条---- */
        this.proBar = this.progressbar.getComponent(cc.ProgressBar);    // 进度条组件
        this.initTime();
        this.loadSource();
        this.comDegress = 0;
        this.comFlag = false;

        // 
        let lv: number = cc.sys.localStorage.getItem('lv');
        if(!lv) {
            lv = 1;
        }
        this.lv_progress_label.string = `当前处于第${lv}关`;
    }

    public onStartBtnClick(e, c): void {
        this.audiomgr.playBtnClickMp3();
        let lv: number = cc.sys.localStorage.getItem('lv');
        if(!lv || parseInt(lv) === 2002) {
            lv = 1;
            cc.sys.localStorage.setItem('lv', 1);
        }
        this.gameNode.active = true;
        this.game.startGame(lv);
        this.homeNode.active = false;
        this.resultNode.active = false;
    }

    public onHomeBtnClick(e, c): void {
        this.audiomgr.playBtnClickMp3();
        this.gameNode.active = false;
        this.resultNode.active = false;
        this.homeNode.active = true;
    }

    public onFinishBtnClick(e, c): void {
        cc.game.end();
        //cc.director.end()
    }

    // ==========================进度条====================================
    public initTime() {
        this.nowTime = 0;       // 当前已经过去得时间
        this.allTime = 10;      // 总时间
    }
 
    public loadSource() {
        cc.loader.loadResDir("/", (compCount, totalCount, item)=>{
            // 进度条回调
            this.comDegress = compCount / totalCount;
        }, (err, assets, urls)=>{
            if (err) {
                cc.error("load sources is err");
            }
            this.comFlag = true;
            this.loadingNode.active = false;
            this.targetNode.active = true;
        });
    }

    update(dt) {
        if(this.comFlag) {
            //
        } else {
            this.proBar.progress = this.comDegress;
            let now = this.comDegress * 100;
            this.progressLabel.string = parseInt(now.toString()) + "%";
        }
    }
}
